The real power of the command line shines through
when you combine the techniques you’ve learned so far with any of
Windows 7’s dozens of command-line tools. I don’t have enough space to
cover every tool, so the rest of
this article takes you through the most useful and powerful
command-line tools in three categories: disk management, file
management, and system management.
Working with Disk Management Tools
Windows
7 comes with a large collection of command-line disk management tools
that enable you to check disks or partitions for errors, as well as
defragment, format, partition, and convert disks. Table 1 lists the disk management tools that you can use with Windows 7.
Table 1. Windows 7’s Command-Line Disk Management Tools
Tool | Description |
---|
CHKDSK | Checks a specified volume for errors. |
CHKNTFS | Configures automatic disk checking. |
CONVERT | Converts a specified volume to a different file system. |
DEFRAG | Defragments a specified volume. |
DISKCOMP | Compares
the contents of two floppy disks. (This tool does not compare hard
disks or other types of removable media, such as memory cards.) |
DISKCOPY | Copies
the contents of one floppy disk to another. (This tool does not copy
hard disks or other types of removable media, such as memory cards.) |
DISKPART | Enables you to list, create, select, delete, and extend disk partitions. |
EXPAND | Extracts one or more files from a compressed file such as a .cab file found on some installation discs. |
FORMAT | Formats the specified volume. |
FSUTIL | Performs a number of file system tasks. |
LABEL | Changes or deletes the name of a specified volume. |
MOUNTVOL | Creates, displays, or deletes a mount point. |
VOL | Displays the name and serial number of a specified volume. |
Note
In this section, I use the word volume to refer to any disk, partition, or mount point.
The next four sections give you more detailed coverage of the CHKDKS, CHKNTFS, and DEFRAG tools.
CHKDSK: Checking for Hard Disk Errors
Check Disk also comes
with a command-line version called CHKDSK that you can run in a Command Prompt window.
Here’s the syntax for CHKDSK:
CHKDSK [volume [filename]] [/F] [/V] [/R] [/B] [/X] [/I] [/C] [/L:[size]]
volume | The drive letter (followed by a colon) or mount point. |
filename | On FAT16 and FAT32 disks, the name of the file to check. Include the path if the file isn’t in the current folder. |
/F | Tells CHKDSK
to automatically fix errors. This is the same as running the Check Disk
GUI with the Automatically Fix File System Errors option activated. |
/V | Runs CHKDSK in verbose mode. On FAT16 and FAT32 drives, CHKDSKCHKDSK displays the path and name of every file on the disk; on NTFS drives, displays cleanup messages, if any. |
/R | Tells CHKDSK to scan the disk surface for bad sectors and recover data from the bad sectors, if possible. (The /F
switch is implied.) This is the same as running the Check Disk GUI with
the Scan for and Attempt Recovery of Bad Sectors option activated. |
/B | Tells CHKDSK
to clear the list of bad sectors on the disk and then recheck the
entire disk. Including this parameter is the same as also including the
/R parameter. |
/X | On
NTFS nonsystem disks that have open files, forces the volume to
dismount, invalidates the open file handles, and then runs the scan.
(The /F switch is implied.) |
/I | On NTFS disks, tells CHKDSK to check only the file system’s index entries. |
/C | On NTFS disks, tells CHKDSK to skip the checking of cycles within the folder structure. This is a rare error, so using /C to skip the cycle check can speed up the disk check. |
/L:[size] | On NTFS disks, tells CHKDSK
to set the size of its log file to the specified number of kilobytes.
The default size is 65,536, which is plenty big enough for most
systems, so you should never need to change the size. Note that if you
include this switch without the size parameter, Check Disk tells you the current size of the log file. |
For example, to run a read-only check—that is, a check that doesn’t repair errors—on the C: drive, you enter the following command:
Note that when you use the /F switch to fix errors, CHKDSK must lock the volume to prevent running processes from using the volume during the check. If you use the /F switch on the %SystemDrive%, which is the drive where Windows 7 is installed (usually drive C:), CHKDSK can’t lock the drive, and you see the following message:
Cannot lock current drive.
Chkdsk cannot run because the volume is in use by another
process. Would you like to schedule this volume to be
checked the next time the system restarts? (Y/N)
If you press Y and Enter, CHKDSK schedules a check for drive C: to run the next time you reboot Windows 7.
CHKNTFS: Scheduling Automatic Disk Checks
You saw in the preceding section that CHKDSK prompts you to schedule an automatic disk check during the next reboot if you run CHKDSK /F on the system drive (usually drive C: in Windows 7).
If you press Y and Enter at these prompts, CHKDSK adds the AUTOCHK utility to the following Registry setting:
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\BootExecute
This setting specifies the programs that Windows 7 should run at boot time when the Session Manager is loading. AUTOCHK is the automatic version of CHKDSK that runs at system startup.
Windows 7 also comes with a command-line tool named CHKNTFS that enables you to cancel pending automatic disk checks, schedule boot-time disk checks without using CHKDSK, and set the time that AUTOCHK counts down before running the automatic disk checks.
Here’s the syntax for CHKNTFS:
CHKNTFS [volume ][/C volume:] [/X volume:] [/D] [/T:[time]]
volume | A drive letter (followed by a colon) or mount point. |
/C
volume | Tells CHKNTFS to schedule an automatic startup disk check for the specified volume. You can specify multiple volumes (separated by spaces). |
/X
volume | Tells CHKNTFS to exclude the specified volume from an automatic startup disk check. You can specify multiple volumes (separated by spaces). |
/D | Tells CHKNTFS to exclude all volumes from an automatic startup disk check. |
/T:[time] | Specifies the time that AUTOCHK counts down before starting the automatic disk checks. |
When you run CHKNTFS with just a volume name, you see one of the following:
If the volume is not scheduled for a startup disk check, you see the volume’s file system:
The type of the file system is NTFS.
If the volume is scheduled for a startup disk check, you see the following message:
Chkdsk has been scheduled manually to run on next reboot.
If Windows 7’s Storage Manager has detected an error on the volume, it marks the volume as dirty, so in this case, you see the following message (using drive C: as an example):
C: is dirty. You may use the /C option to schedule chkdsk for this drive.
This last message is confusing because Windows 7 always performs an automatic startup disk check of any volume that’s marked as dirty. What you can do with CHKNTFS is bypass the automatic startup disk check of any volume that is marked as dirty. To do that, run CHKNTFS with the /X switch, as in this example:
Note
To manually mark a volume as dirty, use the FSUTIL DIRTY SET
volume command, where volume is the drive you want to work with. For example, the following command marks drive C: as dirty:
If you’re not sure whether a drive is dirty, either run CHKNTFS
volume or run FSUTIL DIRTY QUERY
volume, as in this example:
Note, however, that FSUTIL doesn’t give you any way to unmark a drive as dirty.
If a volume isn’t already marked as dirty, you can force CHKDSK to check a volume at startup by running CHKNTFS with the /C switch. For example, the following command sets up an automatic start check for the D: drive:
Note that the /C switch is cumulative, meaning that if you run it multiple times and specify a different volume each time, CHKNTFS
adds each new volume to the list of volumes to check at startup.
Instead of running multiple commands, however, you can specify multiple
volumes in a single command, like so:
If you know a volume has been scheduled for a startup check, but you want to cancel that check, run CHKNTFS with the /X switch, as in this example:
You can also specify multiple volumes, if needed:
If you know that multiple volumes are scheduled for automatic startup checks, you can cancel all the checks by running CHKNTFS with the /D switch:
If
you’ve scheduled a startup check for one or more volumes, or if a
volume is marked as dirty, the next time you reboot Windows 7, you see
a message similar to the following (which uses drive C: as an example):
Checking file system on C:
The type of the file system is NTFS.
Volume label is SYS.
One of your disks needs to be checked for consistency. You
may cancel the disk check, but it is strong recommended
that you continue.
To skip disk checking, press any key within 10 second(s).
The
number of seconds in the last line counts down to 0: If you press a key
before the countdown ends, Windows 7 skips the disk check; otherwise,
it continues with CHKDSK.
Caution
Pressing
any key to skip the disk check usually only works with wired keyboards.
On most wireless keyboards, pressing a key has no effect.
You can change the initial countdown value by running CHKNTFS with the /T
switch, followed by the number of seconds you want to use for the
countdown. For example, the following command sets the countdown to 30
seconds:
Note that if you run the command CHKNTFS /T (that is, you don’t specify a countdown value), CHKNTFS returns the current countdown value.
DEFRAG: Defragmenting the System Drive
If you want to schedule a defragment or perform
this chore from a batch file, you have to use the DEFRAG command-line tool. Here’s the syntax:
DEFRAG disks[/a] [/c] [/e] [/f] [/h] [/r] [/t] [/u] [/v]
disks | Specifies the drive letter (followed by a colon) of each disk you want to defragment (separate multiple drives with a space). |
/a | Tells DEFRAG only to analyze the disk. |
/c | Tells DEFRAG to defragment all the system’s drives. |
/e | Tells DEFRAG to defragment all the system’s drives except those specified with the disks parameter. |
/f | Forces DEFRAG to defragment the disk, even if it doesn’t need defragmenting or if the disk has less than 7% free space. (DEFRAG normally requires at least that much free space because it needs an area in which to sort the files.) |
/h | Runs DEFRAG with a higher program priority for better performance. |
/r | Tells DEFRAG to defragment all the specified drives at the same time (in parallel). |
/t | Tells DEFRAG to show the progress of an already running defrag. |
/u | Tells DEFRAG to show the progress of the defrag. |
/v | Runs DEFRAG in verbose mode, which displays both the analysis report and the defragmentation report. |
Note
To run the DEFRAG utility, you must use an administrator Command Prompt session, as described earlier.
Caution
The DEFRAG switches are case sensitive. So, for example, the following command will work properly:
However, this command will not:
For example, to get an analysis report of the fragmentation of drive C:, enter the following command:
If the volume isn’t too fragmented, you see a report similar to this:
Post Defragmentation Report:
Volume Information:
Volume size = 116.49 GB
Free space = 106.89 GB
Total fragmented space = 1%
Largest free space size = 56.49 GB
Note: File fragments larger than 64MB are not
included in the fragmentation statistics.
You do not need to defragment this volume.
However, if the drive is quite fragmented, you see a report similar to the following:
Post Defragmentation Report:
Volume Information:
Volume size = 397.12 GB
Free space = 198.32 GB
Total fragmented space = 9%
Largest free space size = 158.43 GB
Note: File fragments larger than 64MB are not
included in the fragmentation statistics.
You should defragment this volume.
If you try to defragment a volume that is running low on disk space, DEFRAG displays the following message:
Volume DATA has only 9% free space available for use by Disk Defragmenter.
To run effectively, Disk Defragmenter requires at least 15% usable free space.
There is not enough disk space to properly complete the operation.
Delete some unneeded files on your hard disk, and then try again.
If you can’t delete files from the volume (for example, if this is a Windows 7 data partition), you can try running DEFRAG with the -f switch to force the operation:
Note
Forcing the defrag operation shouldn’t cause problems in most cases. With less free space in which to work, DEFRAG just takes quite a bit longer to defragment the volume, and there may be parts of the volume that it simply can’t defragment.